package main

import (
	"fmt"
	"math/rand"
	"sort"
)

type Struct_hero struct {
	Name string
	Age int
}

type HeroSlice []Struct_hero

//实现interface接口
func (hs HeroSlice) Len() int{
	return len(hs)
}

//less决定你使用什么标准进行排序
//按Age大小排序
func (hs HeroSlice)Less(i, j int) bool {
	return hs[i].Age > hs[j].Age
}

func (hs HeroSlice) Swap(i, j int){
	temp := hs[i]
	hs[i] = hs[j]
	hs[j] = temp
}

func main() {
	var heros HeroSlice
	for i:=0 ;i<10; i++{
		hero := Struct_hero{
			Name : fmt.Sprintf("hero %d", i),
			Age : rand.Intn(100),
		}
		heros = append(heros, hero)
	}

	for _, v := range heros{
		fmt.Println(v)
	}
	fmt.Println()

	//因为heros已经实现了sort.Sort中的接口
	sort.Sort(heros)

	for _, v := range heros{
		fmt.Println(v)
	}
}